package android.itv.view;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.itv.R;
import android.itv.library.Database;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;

public class Login extends Activity
{
    Button btnLogin;
    private EditText edtUser;
    private EditText edtPassword;
    private ImageView imgRegister;
    private CheckBox chbRemember;
    private String account="account";
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        
        Database b=new Database(this);
        b.moveDB();
        
        btnLogin=(Button)findViewById(R.id.btnLogin);
        edtUser=(EditText)findViewById(R.id.edtuser);
        edtPassword=(EditText)findViewById(R.id.edtpassword);
        imgRegister=(ImageView)findViewById(R.id.imgregister);
        chbRemember=(CheckBox)findViewById(R.id.chbRemember);
        
        edtUser.requestFocus();
        
        SharedPreferences sp=PreferenceManager.getDefaultSharedPreferences(this);
        sp=getSharedPreferences(account,MODE_PRIVATE);
        edtUser.setText(sp.getString("USER", ""));
        edtPassword.setText(sp.getString("PASS", ""));
        chbRemember.setChecked(sp.getBoolean("CHECK", false));
        
        
        //Neu' ten dang nhap hoac mat khau sai
        Intent i=getIntent();        
        boolean s=i.getBooleanExtra("ERROR", false);
        if(s)createDialogError();
        btnLogin.setOnClickListener(new View.OnClickListener()
	{
	    
	    @Override
	    public void onClick(View v)
	    {
		if(checkNetworkAvaiable())
		{
        		Intent intent=new Intent(getApplicationContext(),WaitingLogin.class);
        		intent.putExtra("USER", edtUser.getText().toString());
        		intent.putExtra("PASSWORD", edtPassword.getText().toString());
        		intent.putExtra("REMEMBER", chbRemember.isChecked());
        		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        		startActivity(intent);
		}
		else 
		    createDialog();
		
	    }
	});
        
        imgRegister.setOnClickListener(new View.OnClickListener()
	{
	    
	    @Override
	    public void onClick(View v)
	    {
		
		if(checkNetworkAvaiable())
		{
		    Intent intent=new Intent(getApplicationContext(),Register.class);
		    startActivity(intent);
		}
		else 
		    createDialog();
	    }
	});
       
    }
    private void createDialog()
    {
	AlertDialog.Builder builder=new AlertDialog.Builder(this);
	
	builder.setTitle(R.string.txtalter);
	builder.setIcon(android.R.drawable.ic_dialog_info);
	builder.setMessage(R.string.txtmessage);
	builder.setPositiveButton(R.string.txtok, new OnClickListener()
	{
	    public void onClick(DialogInterface dialog, int which)
	    {
		
	    }
	});
	builder.create().show();
    }
    
    private boolean checkNetworkAvaiable()
    {
	ConnectivityManager manager=(ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
	NetworkInfo info=manager.getActiveNetworkInfo();
	if(info!=null && info.isAvailable())
	{
	    return true;
	}
	return false;
    }
    
    private void createDialogError()
    {
	AlertDialog.Builder builder=new AlertDialog.Builder(this);
	
	builder.setTitle(R.string.txtalter);
	builder.setIcon(android.R.drawable.ic_dialog_info);
	builder.setMessage(R.string.txterror);
	builder.setPositiveButton(R.string.txtok, new OnClickListener()
	{
	    public void onClick(DialogInterface dialog, int which)
	    {
		
	    }
	});
	builder.create().show();
    }
    
}
